/*
 * Copyright (c) 2019, salesforce.com, inc.
 * All rights reserved.
 * Licensed under the BSD 3-Clause license.
 * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
 */

import type { DirFileNameSelection, LocalComponent } from '../../util/types';
import { CompositeParametersGatherer, SfCommandlet, SfWorkspaceChecker } from '@salesforce/salesforcedx-utils-vscode';
import { LightningEventOptions, TemplateType } from '@salesforce/templates';
import { Uri } from 'vscode';
import { nls } from '../../messages';
import { salesforceCoreSettings } from '../../settings';
import { MetadataTypeGatherer, SelectFileName, SelectOutputDir } from '../util';
import { OverwriteComponentPrompt } from '../util/overwriteComponentPrompt';
import { FileInternalPathGatherer, InternalDevWorkspaceChecker } from './internalCommandUtils';
import { LibraryBaseTemplateCommand } from './libraryBaseTemplateCommand';
import { AURA_DIRECTORY, AURA_EVENT_EXTENSION, AURA_TYPE } from './metadataTypeConstants';

class LibraryLightningGenerateEventExecutor extends LibraryBaseTemplateCommand<DirFileNameSelection> {
  public executionName = nls.localize('lightning_generate_event_text');
  public telemetryName = 'lightning_generate_event';
  public metadataTypeName = AURA_TYPE;
  public templateType = TemplateType.LightningEvent;
  public getOutputFileName(data: DirFileNameSelection) {
    return data.fileName;
  }
  public getFileExtension() {
    return AURA_EVENT_EXTENSION;
  }
  public constructTemplateOptions(data: DirFileNameSelection) {
    const internal = salesforceCoreSettings.getInternalDev();
    const templateOptions: LightningEventOptions = {
      outputdir: data.outputdir,
      eventname: data.fileName,
      template: 'DefaultLightningEvt',
      internal
    };
    return templateOptions;
  }
}

export const lightningGenerateEvent = (): void => {
  const createTemplateExecutor = new LibraryLightningGenerateEventExecutor();
  const commandlet = new SfCommandlet(
    new SfWorkspaceChecker(),
    new CompositeParametersGatherer<LocalComponent>(
      new MetadataTypeGatherer(AURA_TYPE),
      new SelectFileName(),
      new SelectOutputDir(AURA_DIRECTORY, true)
    ),
    createTemplateExecutor,
    new OverwriteComponentPrompt()
  );
  void commandlet.run();
};

export const internalLightningGenerateEvent = (sourceUri: Uri): void => {
  const createTemplateExecutor = new LibraryLightningGenerateEventExecutor();
  const commandlet = new SfCommandlet(
    new InternalDevWorkspaceChecker(),
    new CompositeParametersGatherer(new SelectFileName(), new FileInternalPathGatherer(sourceUri)),
    createTemplateExecutor
  );
  void commandlet.run();
};
